package com.hustbaidu.hustmap;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.widget.Button;
import android.widget.TextView;

public class GroupTestView extends ViewGroup{
	private MapView mv;
	private InfoWindowView iwv;
	private InfoWindowSurfaceView iwsv;
	private GestureDetector mapGestureDetector;

	public GroupTestView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public GroupTestView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public GroupTestView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.setLongClickable(true);
		initChild(context);
		mapGestureDetector = new GestureDetector(new MapGestureListener());
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
//		return super.onInterceptTouchEvent(ev);
//		System.out.println("view group onInterceptTouchEvent");
		mapGestureDetector.onTouchEvent(ev);
		return false;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
//		System.out.println("onTouchEvent");
//		return mapGestureDetector.onTouchEvent(event);
//		System.out.println("view group onTouchEvent");
		return super.onTouchEvent(event);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// TODO Auto-generated method stub		
		View child = getChildAt(0);
		child.setVisibility(View.VISIBLE);
		child.measure(r - l, b - t);
		child.layout(0, 0, child.getMeasuredWidth(), child .getMeasuredHeight());
		
//		child = getChildAt(1);
//		child.setVisibility(View.VISIBLE);
//		child.measure(r - l, b - t);
//		child.layout(10, 10, child.getMeasuredWidth(), child .getMeasuredHeight());
//		System.out.println(child.getMeasuredWidth() + " : " + child .getMeasuredHeight());
//		child.layout(10, 10, 109, 29);
		
//		View child = getChildAt(2);
		child = getChildAt(2);
		child.setVisibility(View.VISIBLE);
		child.measure(r - l, b - t);
//		System.out.println(child.getMeasuredWidth() + " : " + child .getMeasuredHeight());
		child.layout(((InfoWindowSurfaceView)child).offsetX, ((InfoWindowSurfaceView)child).offsetY, ((InfoWindowSurfaceView)child).offsetX + ((InfoWindowSurfaceView)child).width, ((InfoWindowSurfaceView)child).offsetY + ((InfoWindowSurfaceView)child).height);
//		child.layout(((InfoWindowView)child).offsetX, ((InfoWindowView)child).offsetY, ((InfoWindowView)child).offsetX + ((InfoWindowView)child).width, ((InfoWindowView)child).offsetY + ((InfoWindowView)child).height);
//		child.layout(0, 0, child.getMeasuredWidth(), child .getMeasuredHeight());
	}
	
	private void initChild(Context context){
		mv = new MapView(context);
		this.addView(mv);
		TextView tv = new TextView(context);
		tv.setText("this is a text");
		this.addView(tv);
//		iwv = new InfoWindowView(context, "InfoWindowView test");
//		this.addView(iwv);
		iwsv = new InfoWindowSurfaceView(context, "InfoWindowView test");
		this.addView(iwsv);
	}

	private class MapGestureListener extends SimpleOnGestureListener{
		
		@Override
		public boolean onSingleTapConfirmed(MotionEvent e) {
			// TODO Auto-generated method stub
			
			System.out.println("onSingleTapConfirmed");
			return super.onSingleTapConfirmed(e);
		}

		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			// TODO Auto-generated method stub
			System.out.println("onSingleTapUp");
			return super.onSingleTapUp(e);
		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			// TODO Auto-generated method stub
			
			mv.moveBy((int)distanceX, (int)distanceY);
			mv.updateMapView();
//			iwv.move(mv.realDistanceX, mv.realDistanceY);
//			iwv.requestLayout();
			iwsv.move(mv.realDistanceX, mv.realDistanceY);
//			iwsv.requestLayout();
//			iwv.scrollBy(mv.realDistanceX, mv.realDistanceY);
//			GroupTestView.this.postInvalidate();
			return super.onScroll(e1, e2, distanceX, distanceY);
		}

		@Override
		public boolean onDoubleTap(MotionEvent e) {
			// TODO Auto-generated method stub
			return super.onDoubleTap(e);
		}

		@Override
		public boolean onDoubleTapEvent(MotionEvent e) {
			// TODO Auto-generated method stub
			return super.onDoubleTapEvent(e);
		}

		@Override
		public boolean onDown(MotionEvent e) {
			// TODO Auto-generated method stub
//			System.out.println("onDown");
//			System.out.println(super.onDown(e));
			return super.onDown(e);
//			return false;
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			// TODO Auto-generated method stub
			return super.onFling(e1, e2, velocityX, velocityY);
		}

		@Override
		public void onLongPress(MotionEvent e) {
			// TODO Auto-generated method stub
			super.onLongPress(e);
		}

		@Override
		public void onShowPress(MotionEvent e) {
			// TODO Auto-generated method stub
			super.onShowPress(e);
		}
		
	}
}
